﻿using System.Data;
using Litografia.BusinessEntities;

namespace Litografia.DataObjects
{
    public sealed class DOMaquina : DOBase
    {
        #region Declarations

        private const string getSP = "maquinaGet";
        private const string updateSP = "maquinaUpdate";
        private const string deleteSP = "maquinaDelete";
        private const string addSP = "maquinaAdd";

        #endregion

        #region Methods: Public

        public static BEMaquina.MaquinaDataTable Get(int empresaId)
        {
            var result = new BEMaquina();

            var command = DB.GetStoredProcCommand(getSP);
            DB.AddInParameter(command, "@empresa_id", DbType.Int32, empresaId);
            var dataset = DB.ExecuteDataSet(command);

            // Pasa informacion hacia el BE
            if (dataset != null && dataset.Tables.Count > 0)
            {
                dataset.Tables[0].TableName = result.Tables[0].TableName;
                result.Merge(dataset);
                result.EnforceConstraints = true;
            }
            return result.Maquina;
        }

        public static void Update(BEMaquina.MaquinaRow maquina)
        {
            var command = DB.GetStoredProcCommand(updateSP);
            DB.AddInParameter(command, "@id", DbType.Int32, maquina.Id);
            DB.AddInParameter(command, "@empresa_id", DbType.Int32, maquina.EmpresaId);
            DB.AddInParameter(command, "@nombre", DbType.String, maquina.Nombre);
            DB.AddInParameter(command, "@costo_hora", DbType.Decimal, maquina.CostoxHora);
            DB.ExecuteNonQuery(command);
        }

        public static void Delete(int id)
        {
            var command = DB.GetStoredProcCommand(deleteSP);
            DB.AddInParameter(command, "@id", DbType.Int32, id);
            DB.ExecuteNonQuery(command);
        }

        public static void Add(BEMaquina.MaquinaRow maquina)
        {
            var command = DB.GetStoredProcCommand(addSP);
            DB.AddInParameter(command, "@empresa_id", DbType.Int32, maquina.EmpresaId);
            DB.AddInParameter(command, "@nombre", DbType.String, maquina.Nombre);
            DB.AddInParameter(command, "@costo_hora", DbType.Decimal, maquina.CostoxHora);
            DB.ExecuteNonQuery(command);
        }

        #endregion
    }
}
